#include "DbManager.h"

DbManager::DbManager(QObject *parent)
    : QObject(parent), m_db(QSqlDatabase::addDatabase("QMYSQL"))
{
    m_db.setHostName("127.0.0.1");
    m_db.setPort(3306);
    m_db.setDatabaseName("XueHaiTongHang");
    m_db.setUserName("root");
    m_db.setPassword("abc123");

    if (!m_db.open())
    {
        qDebug() << "数据库连接失败：" << m_db.lastError().text();
    }
    else
    {
        qDebug() << "数据库连接成功";
    }
}

DbManager::~DbManager()
{
    if (m_db.isOpen())
    {
        m_db.close();
    }
}

DbManager &DbManager::instance()
{
    static DbManager instance;
    return instance;
}

QSqlDatabase DbManager::getDatabase() const
{
    return m_db;
}

bool DbManager::isOpen() const
{
    return m_db.isOpen();
}